//
//  BGSubmitCell.swift
//  Billing
//
//  Created by Dale on 17/4/20.
//  Copyright © 2017年 Nari. All rights reserved.
//

import UIKit

typealias ClickClosure = (String, Int)->Void

class BGSubmitCell: UITableViewCell {
    
    
    @IBOutlet weak var leftLB: UILabel!
    @IBOutlet weak var rightLB: UILabel!
    @IBOutlet weak var left1LB: UILabel!
    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var submitBtn: UIButton!
    @IBOutlet weak var lineHeight: NSLayoutConstraint!
    var clickClosure: ClickClosure!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code

        submitBtn?.layer.cornerRadius = 4.0
        submitBtn?.layer.masksToBounds = true
        textField?.delegate = self
        
        if lineHeight != nil {
            lineHeight.constant = 1.0 / UIScreen.main.scale
        }

    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
    @IBAction func handleSubmitBtnAction(_ sender: Any) {
        self.clickClosure("", 1)
    }
    class func cellWithTableView(tableView: UITableView, obj: Any) -> BGSubmitCell {
        let model = obj as! Model
        let cellIdentifier = "BGSubmitCell"
        var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as? BGSubmitCell
        if cell == nil {
            switch model.type {
            case 0:
                cell = Bundle.main.loadNibNamed("BGSubmitCell", owner: nil, options: nil)?[0] as? BGSubmitCell
            case 1:
                cell = Bundle.main.loadNibNamed("BGSubmitCell", owner: nil, options: nil)?[1] as? BGSubmitCell
            default:
                cell = Bundle.main.loadNibNamed("BGSubmitCell", owner: nil, options: nil)?[2] as? BGSubmitCell
            }
        }
        return cell!
    }
    func configureCell(model: Model) -> Void {
        leftLB?.text = model.title
        left1LB?.text = model.title
        rightLB?.text = model.detail
        textField?.text = model.detail
    }
}

extension BGSubmitCell: UITextFieldDelegate {
    public func textFieldDidEndEditing(_ textField: UITextField) {
        self.clickClosure?(textField.text!, 0)
    }
    
    public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if string == "\n" {
            textField.resignFirstResponder()
            return false
        }
        return true
    
}

}

